/**
 * Types for sequential content blocks in messages
 * Supporting reflection->action flow with ordered content rendering
 */

import type { ToolCallResult } from "./openai";

export interface BaseContentBlock {
  id: string;
  type: ContentBlockType;
  timestamp: number;
  order: number;
}

export type ContentBlockType = "reflection" | "tool_call";

export interface ReflectionContentBlock extends BaseContentBlock {
  type: "reflection";
  content: string;
  isComplete: boolean; // Whether the reflection content is fully streamed
}

export interface ToolCallContentBlock extends BaseContentBlock {
  type: "tool_call";
  toolCall: ToolCallResult;
  isExecuting: boolean; // Whether the tool is currently executing
}

export type ContentBlock = ReflectionContentBlock | ToolCallContentBlock;

export interface SequentialMessageContent {
  blocks: ContentBlock[];
  currentBlockId?: string; // ID of the block currently being updated
  isComplete: boolean; // Whether all content is complete
}

// Utility functions for working with content blocks
export const createReflectionBlock = (
  id: string,
  order: number,
  content = "",
): ReflectionContentBlock => ({
  id,
  type: "reflection",
  content,
  timestamp: Date.now(),
  order,
  isComplete: false,
});

export const createToolCallBlock = (
  id: string,
  order: number,
  toolCall: ToolCallResult,
): ToolCallContentBlock => ({
  id,
  type: "tool_call",
  toolCall,
  timestamp: Date.now(),
  order,
  isExecuting: toolCall.status === "pending",
});

export const updateReflectionBlock = (
  block: ReflectionContentBlock,
  content: string,
  isComplete = false,
): ReflectionContentBlock => ({
  ...block,
  content,
  isComplete,
  timestamp: Date.now(),
});

export const updateToolCallBlock = (
  block: ToolCallContentBlock,
  toolCall: ToolCallResult,
): ToolCallContentBlock => ({
  ...block,
  toolCall,
  isExecuting: toolCall.status === "pending",
  timestamp: Date.now(),
});

export const isReflectionBlock = (
  block: ContentBlock,
): block is ReflectionContentBlock => block.type === "reflection";

export const isToolCallBlock = (
  block: ContentBlock,
): block is ToolCallContentBlock => block.type === "tool_call";
